#!/bin/bash

currentdir=$(cd "$(dirname "$0")"; pwd)
echo "build directory: ${currentdir}."

echo -e "\nbuild libevent..."
./autogen.sh
if [ ! -d "$currentdir/build" ]; then mkdir "$currentdir/build"; fi 
CFLAGS=-fPIC ./configure --disable-samples --enable-shared=no --enable-function-sections
make
make install DESTDIR=$currentdir/build
if [ ! -f "$currentdir/build/usr/local/lib/libevent_core.a" ]; then return 1; fi
cp -v $currentdir/build/usr/local/lib/libevent_core.a ${currentdir}/../../../lib
cp -v $currentdir/build/usr/local/lib/libevent_pthreads.a ${currentdir}/../../../lib
cp -v $currentdir/build/usr/local/lib/libevent_extra.a ${currentdir}/../../../lib
cp -v $currentdir/build/usr/local/lib/libevent_openssl.a ${currentdir}/../../../lib
make clean
rm -rf ${currentdir}/build/*

echo "====== libevent build success! ======"
